<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <!-- 浅拷贝就是值的拷贝和引用的拷贝，而深拷贝是把引用类型的值完完全全的拷贝一份，形成一个新的引用类型，互补影响 -->
    <!-- 一般用递归实现深拷贝 -->

    <!-- 更新概念 -->
    <!-- 
        浅拷贝:
            基本数据类型:就是值的拷贝
            复杂数据类型:对象地址的拷贝,也就是拷贝的两个值,指向对象的同一个地址

        深拷贝:拷贝的两个对象拥有不同的地址,修改其中一个属性,其他对象不会受影响


        深拷贝实现方法:
        1、JSON.stringify,确定是无法正确处理函数
        2、判断类型,递归拷贝
        3、lodash.js的_.cloneDeep(value)方法可以实现深拷贝
     -->
    <script>
        function deepClone(target) {

            let result;
            if (typeof target === 'object') {
                if (Array.isArray(target)) {
                    result = []
                    for (let i in target) {
                        result.push(deepClone(target[i]))
                    }
                } else if (target == null) {
                    result = null
                } else if (target.constructor === RegExp) {
                    result = target
                } else {
                    result = {}
                    for (let i in target) {
                        result[i] = deepClone(target[i])
                    }
                }
            } else {
                result = target
            }

            return result
        }

        var obj1 = {
            a: 'a',
            b: 'b',
            array1: [1, 'hello', {
                target: "arr"
            }],
            sayName() {
                console.log("say-name")
            },
        }

        var obj2 = deepClone(obj1)
        obj1.c = 'c'
        obj1.array1.push({
            address: 'beijing'
        })
        console.log(obj1)
        console.log(obj2)

        // for in  便利对象中可枚举属性
        for (const propName in window) {
            console.log(propName)
        }
        // for in 便利对象中可枚举属性
        for (const propName in obj2) {
            console.log(propName)
        }


        var newObj = {}
        for (const pp in obj2) {
            newObj[pp] = obj2[pp]
        }
        console.log('newObj')
        console.log(newObj)
        console.log('newObj')
    </script>

    <script>
        function deeepClone(target) {
            let result = null
            if (typeof target == 'object') {
                if (Array.isArray(target)) {
                    result = []
                    for (let i in target) {
                        array.push(deeepClone(target[i]))
                    }
                } else if (target == null) {
                    result = null
                } else if (target.constructor == RegExp) {
                    result = target
                } else {
                    result = {}
                    for (let i in target) {
                        obj[i] = deeepClone(target[i])
                    }
                }

            } else {
                result = target
            }

            return target
        }
    </script>
</body>

</html>